define(["require", "exports", "../mve/childrenBuilder", "./index"], function (require, exports, childrenBuilder_1, index_1) {
    "use strict";
    exports.__esModule = true;
    exports.strToFun = exports.mergeFromTextChild = exports.Text = exports.SvgFragment = exports.mergeFromEOChild = exports.Svg = exports.Fragment = exports.mergeFromTsxChildren = exports.fromTsxChildren = exports.fromTsxChild = exports.Dom = exports.createElement = void 0;
    /**
     * 简单的tsx支撑
     * 如在tsconfig中配置
            "jsx": "react",
            "jsxFactory":"createElement",
     * 需要的地方导入
            import { createElement,Dom,Fragment,JSX, Svg, SvgFragment } from 'mve-dom/tsxSupport'
     * 可能需要全局导入
            declare namespace JSX {
                type IntrinsicElements=import("mve-dom/tsxSupport").JSX.IntrinsicElements
                type Element=import("mve-dom/tsxSupport").JSX.Element
                type ElementChildrenAttribute=import('mve-dom/tsxSupport').JSX.ElementChildrenAttribute
            }
     */
    function createElement(type) {
        var vs = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            vs[_i - 1] = arguments[_i];
        }
        return type.apply(null, vs);
    }
    exports.createElement = createElement;
    function isEOChild(v) {
        return typeof (v) == 'function';
    }
    function Dom(props) {
        var children = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            children[_i - 1] = arguments[_i];
        }
        props.children = mergeFromTsxChildren(props.children, children);
        return (0, index_1.dom)(props);
    }
    exports.Dom = Dom;
    function fromTsxChild(child) {
        if (isEOChild(child)) {
            return child;
        }
        else {
            //字符串、布尔、数字的情况
            return (0, index_1.domText)({
                text: child + ""
            });
        }
    }
    exports.fromTsxChild = fromTsxChild;
    function fromTsxChildren(children) {
        var length = children.length;
        for (var i = 0; i < length; i++) {
            var child = children[i];
            children[i] = fromTsxChild(child);
        }
        return children;
    }
    exports.fromTsxChildren = fromTsxChildren;
    function mergeFromTsxChildren(childrenA, childrenB) {
        if (childrenA) {
            var allChildren = mb.Array.isArray(childrenA) ? fromTsxChildren(childrenA) : [fromTsxChild(childrenA)];
            for (var _i = 0, childrenB_1 = childrenB; _i < childrenB_1.length; _i++) {
                var child = childrenB_1[_i];
                allChildren.push(fromTsxChild(child));
            }
            return allChildren;
        }
        else {
            return fromTsxChildren(childrenB);
        }
    }
    exports.mergeFromTsxChildren = mergeFromTsxChildren;
    function Fragment(props) {
        var children = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            children[_i - 1] = arguments[_i];
        }
        props = props || {};
        props.children = mergeFromTsxChildren(props.children, children);
        return (0, childrenBuilder_1.fragment)(props);
    }
    exports.Fragment = Fragment;
    function Svg(props) {
        var children = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            children[_i - 1] = arguments[_i];
        }
        props.children = mergeFromEOChild(props.children, children);
        return (0, index_1.svg)(props);
    }
    exports.Svg = Svg;
    function mergeFromEOChild(childrenA, childrenB) {
        if (childrenA) {
            var allChildren = mb.Array.isArray(childrenA) ? childrenA : [childrenA];
            for (var _i = 0, childrenB_2 = childrenB; _i < childrenB_2.length; _i++) {
                var child = childrenB_2[_i];
                allChildren.push(child);
            }
            return allChildren;
        }
        else {
            return childrenB;
        }
    }
    exports.mergeFromEOChild = mergeFromEOChild;
    function SvgFragment(props) {
        var children = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            children[_i - 1] = arguments[_i];
        }
        props = props || {};
        props.children = mergeFromEOChild(props.children, children);
        return (0, childrenBuilder_1.fragment)(props);
    }
    exports.SvgFragment = SvgFragment;
    function Text(props) {
        var children = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            children[_i - 1] = arguments[_i];
        }
        props = props || {};
        var nprops = props;
        nprops.text = mergeFromTextChild(props.children, children);
        return (0, index_1.domText)(nprops);
    }
    exports.Text = Text;
    function mergeFromTextChild(childrenA, childrenB) {
        var isAllText = true;
        var allChildren = childrenA ? (Array.isArray(childrenA) ? childrenA : [childrenA]) : [];
        for (var _i = 0, allChildren_1 = allChildren; _i < allChildren_1.length; _i++) {
            var child = allChildren_1[_i];
            if (typeof (child) == 'function') {
                isAllText = false;
            }
        }
        for (var _a = 0, childrenB_3 = childrenB; _a < childrenB_3.length; _a++) {
            var child = childrenB_3[_a];
            allChildren.push(child);
            if (typeof (child) == 'function') {
                isAllText = false;
            }
        }
        if (allChildren.length > 0) {
            if (allChildren.length == 1) {
                return allChildren[0];
            }
            else {
                if (isAllText) {
                    return allChildren.join("");
                }
                else {
                    for (var i = 0; i < allChildren.length; i++) {
                        allChildren[i] = strToFun(allChildren[i]);
                    }
                    var transChildren_1 = allChildren;
                    return function () {
                        return transChildren_1.map(function (child) { return child(); }).join("");
                    };
                }
            }
        }
        else {
            return "";
        }
    }
    exports.mergeFromTextChild = mergeFromTextChild;
    function strToFun(child) {
        if (typeof (child) != 'function') {
            return function () {
                return child;
            };
        }
        return child;
    }
    exports.strToFun = strToFun;
});
